extends Node2D
 
func _ready():
	# 摄像机
	var tilemap :TileMap = $TileMap
	var tile_size = tilemap.get_tileset().get_tile_size()
	var rect = tilemap.get_used_rect()
	var bound = Rect2(
		rect.position.x * tile_size.x,
		rect.position.y * tile_size.y,
		rect.size.x * tile_size.x,
		rect.size.y * tile_size.y,
	)
	
	# 摄像机范围限定
	var camera :Camera2D = $Player/Camera2D
	camera.limit_left =  bound.position.x 
	camera.limit_top = bound.position.y  
	camera.limit_right = bound.end.x
	camera.limit_bottom = bound.end.y
	camera.reset_smoothing()
	
	# 创建一个hit box， 触发掉落的怪物和玩家死亡事件
	if $BottomDeath != null:
		var shape2d = RectangleShape2D.new()
		shape2d.set_size(Vector2(bound.size.x,tile_size.y))
		$BottomDeath/CollisionShape2D.set_shape(shape2d)
		$BottomDeath.position = Vector2(
			bound.position.x + bound.size.x/2, # 横坐标中心
			bound.end.y + tile_size.y*2 # 纵坐标底部再下移两个单元
		)
		
	# 收集金币
	var hud :PackedScene = preload("res://scenes/ui/hud.tscn")
	add_child(hud.instantiate())

func _input(event):
	if event.is_action_pressed("ui_cancel"):
		Global.back_to_title()
